//
//  GameScene.swift
//  ShapeSurvival
//
//  Created by Jerry H on 1/7/15.
//  Copyright (c) 2015 HeylmunProgramming. All rights reserved.
//

import SpriteKit

var label1 = SKLabelNode(fontNamed:"Ariel")
var scoreLabel = SKLabelNode(fontNamed:"Ariel")
//var player: Player = Player(imageNamed: "Player");
var player: Player = Player(imageNamed: "Player");
var leftThumbStick: ThumbStick = ThumbStick();
var background: backgroundHandler = backgroundHandler();

var playScene: AnyObject = 0
var eManager: EnemyManager = EnemyManager();
var score: Int = 0
var highX: CGFloat = 0
var lowX: CGFloat = 0
var highY: CGFloat = 0
var lowY: CGFloat = 0
var startTime: CGFloat = -1.00;
var runTime: CGFloat = 0.00;

var currentBullets: [Bullet] = [];
var momentum: Double = 0.25
var lastShot: CFTimeInterval = 0.00
var shooting = false;
var overheat: Double = 0.00;
var overheating = false;

//var eManager: EnemyManager; // = EnemyManager(self)


class GameScene: SKScene {
    
    
    
    func getSelf() -> GameScene{
        return self
    }
    
    override func didMoveToView(view: SKView) {
        playScene = self
        
        label1.text = "";
        label1.fontSize = 30;
        label1.position = CGPoint(x:CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame));
        label1.zPosition = 0.85
        self.addChild(label1)
        
        scoreLabel.text = "0";
        scoreLabel.fontSize = 30;
        scoreLabel.position = CGPoint(x: (CGRectGetMinX(self.frame) + 55), y: CGFloat(CGRectGetMaxY(self.frame) - 28)) ;
        scoreLabel.zPosition = 0.88
        self.addChild(scoreLabel)
        
        player.playerSet(self)
        self.addChild(player)
        
        background.setup(self)
        
        highX = (CGRectGetMaxX(self.frame) * 1.2 )
        lowX = ( 0 - ( (CGRectGetMaxX(self.frame) * 1.2) - (CGRectGetMaxX(self.frame) )))
        highY = (CGRectGetMaxY(self.frame) * 1.2)
        lowY = ( 0 - ( (CGRectGetMaxY(self.frame) * 1.2) - (CGRectGetMaxY(self.frame) )))
        
        
        
    }
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */
        
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            //label1.text = (" x: \(location.x.description) , y: \(location.y.description) ");
            
            if(location.x < CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)){
                leftThumbStick.touchStart(location, scene: self)
                
            }
            else if(location.x > CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)){
                //right side, shoot
                if( shooting == false){
                    shooting = true
                }
                else {
                    shooting = false
                }
            }
        }
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            if(location.x < CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)){
                leftThumbStick.removeThumbStick();
            }
        }
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            
            let location = touch.locationInNode(self)
            if(location.x < CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)){
                leftThumbStick.stickMoved(location)
            }
        }
    }
    
    func updateTime(time: CFTimeInterval){
        if(startTime == -1){
            startTime = CGFloat(time);
        }
        runTime = CGFloat(time) - startTime
    }
    
    func updateExplosions(){
        for explosion: Explosion in eManager.futureExplosions {
            explosion.startExplosion(self)
        }
        eManager.futureExplosions.removeAll(keepCapacity: false)
    }
    

    
    func shoot(){
        let temp: Bullet = Bullet(imageNamed: "Bullet");
        //let temp: Bullet = Bullet(imageNamed: "Player");
        currentBullets.append(temp)
        temp.setUp()
        self.addChild(temp)
    }
    
    func moveBullets(){
        for bullet: Bullet in currentBullets {
            if(bullet.inBounds() == false){
                let tempP = find(currentBullets, bullet)
                var setP = -1
                if(tempP? != nil){
                    setP = Int(tempP!)
                }
                currentBullets.removeAtIndex(setP)
                bullet.removeFromParent()
            }
            else {
                bullet.moveBullet();
                bullet.checkHits();
            }
        }
    }
    
    func setMessage(inp: String, time: NSTimeInterval){
        label1.text = inp
        label1.runAction(SKAction.fadeAlphaTo(1.00, duration: 0.00))
        label1.runAction(SKAction.fadeAlphaTo(0.00, duration: time))
    }
    
    

    
    func checkFiring(){
        if(runTime > CGFloat(lastShot + 0.02)){
            if(overheating == false) {
                if(shooting == true){
                    shoot()
                    lastShot = CFTimeInterval(runTime)
                    overheat = overheat + 1
                    if(overheat == 100){
                        overheating = true
                        shooting = false
                        setMessage("OVERHEATING", time: 4.75)
                        
                    }
                }
                else{
                    let n = overheat - 2
                    if( n <= 0){
                        overheat = 0
                    }
                    else{
                        overheat = overheat - 2
                        lastShot = CFTimeInterval(runTime)
                    }
                }
            }
            else {
                overheat = overheat - 0.75
                lastShot = CFTimeInterval(runTime)
                if(overheat <= 0){
                    overheat = 0
                    shooting = false
                    overheating = false
                }
            }
        }
    }
    
    override func update(currentTime: CFTimeInterval) {
        scoreLabel.text = ("\(score)")
        player.playerMove()
        updateTime(currentTime)
        eManager.spawnEnemies()
        eManager.moveEnemies()
        moveBullets()
        checkFiring()
        updateExplosions()
        background.refresh(self)
        
    }
}
